<template>
  <div class="app-container-component">
    <el-form :model="searchForm" label-width="100px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="other">
          <el-form-item label="名称" size="small">
            <el-input v-model="searchForm.name"></el-input>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" v-if="roleType===0 || roleType===10" @click="addTeam()">添加</el-button>
      <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="700px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="110px">
          <el-row>
            <el-col class="singleDatePicker">
              <el-form-item prop="name" label="名称" size="small">
                <el-input v-model="teamRow.name"></el-input>
              </el-form-item>
              <el-form-item label="用途" size="small">
                <el-input v-model="teamRow.user"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item prop="model" label="型号" size="small">
                <el-input v-model="teamRow.model"></el-input>
              </el-form-item>
              <el-form-item label="购置日期" size="small">
                <el-date-picker
                  v-model="teamRow.buyTime"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="出厂信息" size="small">
            </el-form-item>
            <el-col class="singleDatePicker">
              <el-form-item prop="manufacturer" label="生产厂家" size="small">
                <el-input v-model="teamRow.manufacturer"></el-input>
              </el-form-item>
              <el-form-item prop="code" label="出厂编号" size="small">
                <el-input v-model="teamRow.code"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item prop="date" label="出厂日期" size="small">
                <el-date-picker
                  v-model="teamRow.date"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="基本参数" size="small">
            </el-form-item>
            <el-col class="singleDatePicker">
              <el-form-item label="材质" size="small">
                <el-input v-model="teamRow.material"></el-input>
              </el-form-item>
              <el-form-item label="设备重量" size="small">
                <el-input v-model="teamRow.weight"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item label="外形尺寸" size="small">
                <el-input v-model="teamRow.overallDimensions"></el-input>
              </el-form-item>
              <el-form-item label="其他技术信息" size="small">
                <el-input v-model="teamRow.otherInfo"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-form-item label="维护与保养" size="small">
            </el-form-item>
            <el-col class="singleDatePicker">
              <el-form-item prop="maintainDate" label="日期" size="small">
                <el-date-picker
                  v-model="teamRow.maintainDate"
                  type="date"
                  value-format="timestamp"
                  placeholder="选择日期">
                </el-date-picker>
              </el-form-item>
              <el-form-item prop="content" label="内容" size="small">
                <el-input v-model="teamRow.content"></el-input>
              </el-form-item>
            </el-col>
            <el-col class="singleDatePicker">
              <el-form-item prop="type" label="类型" size="small">
                <el-select v-model="teamRow.type" placeholder="请选择">
                  <el-option
                    v-for="item in global.equipmentarchiveTypeList"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-form-item prop="designatedArea" label="存放地" size="small">
            <el-input v-model="teamRow.designatedArea"></el-input>
          </el-form-item>
          <el-form-item label="说明书" size="small">
            <input type="file" ref='instructionsFile' accept="image/*" @change="fileUpload($event,'instructionsFile')">
          </el-form-item>
          <el-form-item label="设备图片1" size="small">
            <input type="file" ref='pictureOneFile' accept="image/*" @change="fileUpload($event,'pictureOneFile')">
          </el-form-item>
          <el-form-item label="设备图片2" size="small">
            <input type="file" ref='pictureTwoFile' accept="image/*" @change="fileUpload($event,'pictureTwoFile')">
          </el-form-item>
          <el-form-item label="备注说明" size="small">
            <el-input type="textarea" v-model="teamRow.remark"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="outerVisible = false">取消</el-button>
        </div>
      </el-dialog>
      <!--<el-button size="small" icon="el-icon-upload2">导出</el-button>-->
    </div>
    <my-table
      :data="tableData"
      :col-configs="colConfigs"
      v-loading="listLoading"
      @openVisible="openVisible"
    >
      <el-table-column
        slot="opt"
        label="操作"
        fixed="right"
        width="120"
        v-if="roleType===0 || roleType===10"
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="editTeam(scope.row.archiveId)"
            type="text"
            size="small">
            修改
          </el-button>
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-equipmentarchive${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-equipmentarchive${scope.$index}`].doClose()">
                取消
              </el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-equipmentarchive${scope.$index}>删除
            </el-button>
          </el-popover>
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog title="会议室照片" :visible.sync="imgVisible" width="600px">
      <img class="img-dialog" :src="`${global.exportUrl}${imgMsg.src}`" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import {
    getList,
    addEquipmentArchive,
    updateEquipmentArchive,
    selectEquipmentArchive,
    delEquipmentArchive
  } from '@/api/equipmentarchive1'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import { Message } from 'element-ui'
  import { AreaSelect } from 'vue-area-linkage'
  import { formatDate } from '@/utils/index'

  const time = {
    props: ['colConfig'],
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :width="colConfig.width">
      <span slot-scope="scope">
        {{ scope.row[colConfig.prop] | formatDate }}
      </span>
  </el-table-column>
  `
  }

  const filePath = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    template: `
  <el-table-column :label="colConfig.label" :min-width="colConfig.minWidth">
      <span slot-scope="scope">
        <div>
          <a v-if="scope.row[colConfig.prop]" @click="openVisible(colConfig.label,scope.row[colConfig.prop])">查看</a>
          <a v-if="scope.row[colConfig.prop]" target="_blank" :href="GLOBAL.filePathUrl1+ encodeURI(scope.row[colConfig.prop])">下载</a>
        </div>
      </span>
  </el-table-column>
  `
  }

  const configs = {
    props: ['colConfig'],
    methods: {
      openVisible(label, src) {
        const msg = {
          label: label,
          src: src
        }
        this.$emit('openImgVisible', msg)
      }
    },
    filters: {
      formatDate(time) {
        var date = new Date(time)
        return formatDate(date, 'yyyy-MM-dd')
      },
      formatter(prop) {
        if (prop) {
          let obj = {}
          obj = global.equipmentarchiveTypeList.find((item) => {
            return item.value === prop // 筛选出匹配数据
          })
          return obj.label
        }
      }
    },
    template: `
  <el-table-column :label="colConfig.label">
            <el-table-column  v-for="(item, index) in colConfig.children"  :label="item.label" :width="item.width" :show-overflow-tooltip="true">
                <span slot-scope="scope" >
                    <span v-if="item.component==='time'"> {{ scope.row[item.prop] | formatDate }}</span>
                    <span v-else-if="item.component==='formatter'"> {{ scope.row[item.prop] | formatter }}</span>
                    <span v-else >{{scope.row[item.prop]}}</span>
                </span>
            </el-table-column>
  </el-table-column>
  `
  }

  export default {
    components: {
      myTable,
      AreaSelect
    },
    data() {
      return {
        colConfigs: [
          { prop: 'name', label: '名称', width: '100' },
          { prop: 'model', label: '型号', width: '100' },
          { prop: 'user', label: '用途', width: '100' },
          { prop: 'buyTime', label: '购置日期', width: '100', component: time },
          {
            label: '出厂信息', children: [
              { prop: 'manufacturer', label: '生产厂家', width: '150' },
              { prop: 'date', label: '出厂日期', width: '100', component: 'time' },
              { prop: 'code', label: '出厂编号', width: '150' }
            ], component: configs
          },
          {
            label: '基本参数', children: [
              { prop: 'material', label: '材质', width: '150' },
              { prop: 'overallDimensions', label: '外形尺寸', width: '100' },
              { prop: 'weight', label: '设备重量', width: '150' },
              { prop: 'otherInfo', label: '其他技术信息', width: '150' }
            ], component: configs
          },
          {
            label: '维护与保养', children: [
              { prop: 'maintainDate', label: '日期', width: '100', component: 'time' },
              { prop: 'type', label: '类型', width: '100', component: 'formatter' },
              { prop: 'content', label: '内容', width: '150' }
            ], component: configs
          },
          { prop: 'designatedArea', label: '存放地', width: '150' },
          { prop: 'instructions', label: '说明书', width: '100', component: filePath },
          { prop: 'pictureOne', label: '设备图片1', width: '100', component: filePath },
          { prop: 'pictureTwo', label: '设备图片2', width: '100', component: filePath },
          { prop: 'remark', label: '备注说明', width: '150' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        typeNames: [],
        listLoading: true,
        dialogTitle: '新增设备档案',
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        outerVisible: false,
        rules: {
          name: [
            { required: true, message: '请输入名称', trigger: 'change' }
          ],
          model: [
            { required: true, message: '请输入型号', trigger: 'change' }
          ],
          manufacturer: [
            { required: true, message: '请输入生产厂家', trigger: 'change' }
          ],
          date: [
            { required: true, message: '请选择出厂日期', trigger: 'change' }
          ],
          code: [
            { required: true, message: '请输入出厂编号', trigger: 'change' }
          ],
          maintainDate: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ],
          type: [
            { required: true, message: '请选择类型', trigger: 'change' }
          ],
          content: [
            { required: true, message: '请输入内容', trigger: 'change' }
          ],
          designatedArea: [
            { required: true, message: '请输入存放地', trigger: 'change' }
          ]
        },
        visible: false,
        imgVisible: false,
        imgMsg: {}
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      roleType() {
        return this.$store.state.user.roleType
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          console.log(response)
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      fileClear() {
        if (this.$refs.instructionsFile) {
          this.$refs.instructionsFile.value = ''
        }
        if (this.$refs.pictureOneFile) {
          this.$refs.pictureOneFile.value = ''
        }
        if (this.$refs.pictureTwoFile) {
          this.$refs.pictureTwoFile.value = ''
        }
      },
      addTeam() {
        this.outerVisible = true
        this.dialogTitle = '新增设备档案'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.dialogType = 1
        this.fileClear()
      },
      editTeam(archiveId) {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.outerVisible = true
        this.dialogTitle = '修改设备档案'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          archiveId: archiveId
        }
        selectEquipmentArchive(params).then(response => {
          this.teamRow = Object.assign({}, this.teamRow, response.responseBody)
        })
        this.fileClear()
      },
      submitForm(formName) {
        const params = new FormData()
        params.append('name', this.teamRow.name || '')
        params.append('model', this.teamRow.model || '')
        params.append('user', this.teamRow.user || '')
        if (this.teamRow.buyTime) {
          params.append('buyTime', this.teamRow.buyTime)
        }
        params.append('manufacturer', this.teamRow.manufacturer || '')
        params.append('date', this.teamRow.date)
        params.append('code', this.teamRow.code || '')
        params.append('material', this.teamRow.material || '')
        params.append('overallDimensions', this.teamRow.overallDimensions || '')
        params.append('weight', this.teamRow.weight || 0)
        params.append('otherInfo', this.teamRow.otherInfo || '')
        params.append('maintainDate', this.teamRow.maintainDate)
        params.append('type', this.teamRow.type || '')
        params.append('content', this.teamRow.content || '')
        params.append('designatedArea', this.teamRow.designatedArea || '')
        if (this.teamRow.instructionsFile) {
          params.append('instructionsFile', this.teamRow.instructionsFile)
        }
        if (this.teamRow.pictureOneFile) {
          params.append('pictureOneFile', this.teamRow.pictureOneFile)
        }
        if (this.teamRow.pictureTwoFile) {
          params.append('pictureTwoFile', this.teamRow.pictureTwoFile)
        }
        params.append('remark', this.teamRow.remark || '')
        if (!this.dialogType) {
          params.append('archiveId', this.teamRow.archiveId)
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              addEquipmentArchive(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            } else {
              updateEquipmentArchive(params).then(response => {
                this.outerVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        this.$refs[`popover-equipmentarchive${scope.$index}`].doClose()
        const params = {
          archiveId: scope.row.archiveId,
          isDel: 1
        }
        delEquipmentArchive(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      fileUpload(event, teamRowName) {
        this.$set(this.teamRow, teamRowName, event.target.files[0])
      },
      openVisible(msg) {
        this.imgVisible = true
        this.imgMsg = msg
      }
    },
    mounted() {
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container-component {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
    .item-inline {
      display: inline-block;
      width: 400px;
    }
    .img-dialog {
      max-width: 540px;
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  a, a:focus, a:hover {
    color: #409EFF;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
